<template>
  <div class="goods-list">

<!-- <router-link :to="'/home/goodsinfo/'+item.id" class="goods-item" v-for="item in goods" :key="item.id"> -->
    <div @click="toInfo(item.id)" class="goods-item" v-for="item in goods" :key="item.id">
      <img :src="item.img_url" alt>
      <h1 class="title">{{item.title}}</h1>
      <div class="info">
        <p class="price">
          <span class="now">￥{{item.sell_price}}</span>
          <span class="old">&yen;{{item.market_price}}</span>
        </p>
        <p class="sell">
          <span>热卖中</span>
          <span>剩{{item.stock_quantity}}件</span>
        </p>
      </div>
    </div>



    <mt-button type="danger" size="large" @click="getMore">加载更多</mt-button>
  </div>
</template>

<script>
export default {
  data() {
    return { pageIndex: 1, goods: [] };
  },
  created() {
    this.getGoodsList();
  },
  methods: {
    getMore() {
      this.pageIndex++;
      this.getGoodsList();
    },
    getGoodsList() {
      this.$http
        .get("goods/list?pageIndex=" + this.pageIndex)
        .then(response => {
          if (response.body.status === 0) {
            // this.goods = response.body.message;
            this.goods = this.goods.concat(response.body.message);
          }
        });
    },
    toInfo(id){
        // 使用js的形式进行路由导航
        //注意 区分 this.$route 和this.$router 这两个对象
        // this.$route是路由【参数对象】，所有路由中的参数，params，query都属于它
        // this.$router是一个路由【导航对象】，用它使用JS代码，实现路由的前进，后退，跳转到新的URL地址
        
        
        this.$router.push("/home/goodsinfo/"+id)
    }
  }
};
</script>

<style lang="scss" scoped>
.goods-list {
  display: flex;
  flex-wrap: wrap;
  padding: 7px;
  justify-content: space-between;

  .goods-item {
    width: 47%;
    border: 1px solid #ccc;
    box-shadow: 0 0 8px #ccc;
    margin: 4px;
    img {
      width: 100%;
    }
    .title {
      font-size: 14px;
    }
    .info {
      background-color: #eee;
      p {
        margin: 0;
        padding: 5px;
      }
      .price {
        .now {
          color: #f00;
          font-weight: bold;
          font-size: 16px;
        }
        .old {
          text-decoration: line-through;
          font-size: 12px;
          margin-left: 10px;
        }
      }
      .sell {
        display: flex;
        justify-content: space-between;
      }
    }
  }
}
</style>
